<?php

/**************************************************/
/* Programmer: Dimasd Widjaya                     */
/*       Date: 2/5/2013                           */
/*  Page Name: webservices.php                    */
/*                                                */
/**************************************************/

/**************************************************/
/* Collection of webservices and related utils    */
/*                                                */
/**************************************************/

/**************************************************/
/*   Date        Reviser       Revision           */
/* --------     ---------     ----------          */
/*                                                */
/*                                                */
/**************************************************/

@ $COMPUTER_NAME = $_SERVER['COMPUTERNAME'];

// Server configuration
if ($COMPUTER_NAME === 'CSWEB')
{
	$SERVER_ADDRESS = 'csweb.studentnet.int';
	$SERVER_USERNAME = 'cs414';
	$SERVER_PASSWORD = 'pcc1234';
}
else if ($COMPUTER_NAME === 'PRAENOMEN')
{
	$SERVER_ADDRESS = 'localhost';
	$SERVER_USERNAME = 'root';
	$SERVER_PASSWORD = 'test_system';
}
else if($COMPUTER_NAME === 'BB-PC')
{
	$SERVER_ADDRESS = 'localhost';
	$SERVER_USERNAME = 'root';
	$SERVER_PASSWORD = 'google';
}
else
{
	$SERVER_ADDRESS = 'localhost';
	$SERVER_USERNAME = 'root';
	$SERVER_PASSWORD = '';
}

function webservices_main()
{
    // Get the name of the function being called by JQuery AJAX
    @$func_name = $_POST['Func'];
    // Execute the function being called by JQuery AJAX
    @call_user_func($func_name, '');
}
	
// Created by: Dimas Widjaya
// Date: 2/9/2013
//
// Call a stored procedure
// First variable will ALWAYS be the name of the procedure being called, the rest will be any parameters needed 
// for the stored procedure

function CallProcedure()
{
	

	$mysqli    = new mysqli($GLOBALS['SERVER_ADDRESS'],$GLOBALS['SERVER_USERNAME'], $GLOBALS['SERVER_PASSWORD'], "cs414");
	$CallQuery = "call "; 
	
	$ResultArray = array();
	
	if ($mysqli->connect_errno) 
	{
		echo "Failed to connect to database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
	}
	
	$indexes = func_num_args();
	$arguments = func_get_args();
	
	$CallQuery .= ($arguments[0] . '(');
	
	for($index = 1; $index < $indexes; $index++)
	{
		if(gettype($arguments[$index]) == 'string')
			$CallQuery .= ("'" . $arguments[$index] . "'");
		else
			$CallQuery .= $arguments[$index];
			
		if($index != $indexes - 1)
			$CallQuery .= ', ';
	}
	
	$CallQuery .= ')';
	
	if(!($result = $mysqli->query($CallQuery)))
	{
		echo $mysqli->error;
		exit();
	}

	if(!(is_bool($result)))
	{
		while($row = $result->fetch_assoc())
		{
			array_push($ResultArray, $row);
		}
	}
			
	return $ResultArray;
}


// Created by: Dimas Widjaya
// Date: 2/9/2013
//
// Get the information needed for user dashboard
function Get_user_dashboard_info($UserID = null)
{
	if(isset($_POST['Func']))
	{
		$UserID = $_POST['UserID'];
		echo json_encode(CallProcedure('get_user_dashboard_info', $UserID));
	}
	else 
		return CallProcedure('get_user_dashboard_info', $UserID);
		
}


// Created by: Dimas Widjaya
// Date: 2/11/2013
//
// Get the list of all user types
function Get_all_user_type()
{
	if(isset($_POST['Func']))
	{
		echo json_encode(CallProcedure('get_all_user_type'));
	}
	else 
		return CallProcedure('get_all_user_type');
		
}

// Created by: Dimas Widjaya
// Date: 2/11/2013
//
// Get the list of all students
function Get_all_students($CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		echo json_encode(CallProcedure('get_all_students_for_enroll', $CourseID));
	}
	else 
		return CallProcedure('get_all_students_for_enroll', $CourseID);
		
}

// Created by: Dimas Widjaya
// Date: 2/11/2013
//
// Get the list of all teachers
function Get_all_teachers($CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		echo json_encode(CallProcedure('get_all_teachers', $CourseID));
	}
	else 
		return CallProcedure('get_all_teachers', $CourseID);
		
}


// Created by: Dimas Widjaya
// Date: 2/12/2013
//
// Get the list of all active courses
function Get_all_active_courses()
{
	if(isset($_POST['Func']))
	{
		echo json_encode(CallProcedure('get_all_active_courses'));
	}
	else 
		return CallProcedure('get_all_active_courses');
		
}

// Created by: Dimas Widjaya
// Date: 2/12/2013
//
// Get the teacher that is teaching a particular class
function Get_teacher_of_a_course($CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		echo json_encode(CallProcedure('get_teacher_of_a_course', $CourseID));
	}
	else 
		return CallProcedure('get_teacher_of_a_course', $CourseID);
		
}

// Created by: Dimas Widjaya
// Date: 2/12/2013
//
// Get all students that is taking a particular class
function Get_students_of_a_course($CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		echo json_encode(CallProcedure('get_students_of_a_course', $CourseID));
	}
	else 
		return CallProcedure('get_students_of_a_course', $CourseID);
		
}

// Created by: Dimas Widjaya
// Date: 2/14/2013
//
// Enroll a user to a course
function Enroll_user_to_a_course($UserID = null, $CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		$UserID = $_POST['UserID'];
		CallProcedure('enroll_user_to_a_course', $UserID, $CourseID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('enroll_user_to_a_course', $UserID, $CourseID);
		
}

// Created by: Dimas Widjaya
// Date: 2/15/2013
//
// Remove a user from a course
function Remove_user_from_a_course($UserID = null, $CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		$UserID = $_POST['UserID'];
		CallProcedure('remove_user_from_a_course', $UserID, $CourseID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('remove_user_from_a_course', $UserID, $CourseID);
		
}

// Created by: Dimas Widjaya
// Date: 2/23/2013
//
// Get Basic info of a test
function Get_test_basic_info($TestID = null, $UserID = null)
{
	
	if(isset($_POST['Func']))
	{
		$TestID = $_POST['TestID'];
		$UserID = $_POST['UserID'];
		CallProcedure('get_test_basic_info', $TestID, $UserID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('get_test_basic_info', $TestID, $UserID);
		
}

// Created by: Dimas Widjaya
// Date: 2/23/2013
//
// Get the questions related to a test
function Get_test_questions($TestID = null, $UserID = null)
{
	

	if(isset($_POST['Func']))
	{
		$TestID = $_POST['TestID'];
		$UserID = $_POST['UserID'];
		CallProcedure('get_test_questions', $TestID, $UserID);
		echo json_encode((object) null);
	}
	else 
	{
		$ErrorMsg = '';
		return CallProcedure('get_test_questions', $TestID, $UserID);
	}
}

// Created by: Dimas Widjaya
// Date: 2/25/2013
//
// Insert student's answer of a test question
function Answer_test_question($Answer = null, $QuestionID = null, $TestRecordID = null)
{
	if(isset($_POST['Func']))
	{
		$Answer = $_POST['Answer'];
		$QuestionID = $_POST['QuestionID'];
		$TestRecordID = $_POST['TestRecordID'];
		CallProcedure('answer_test_question', $Answer, $QuestionID, $TestRecordID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('answer_test_question', $Answer, $QuestionID, $TestRecordID);
}

// Created by: Dimas Widjaya
// Date: 3/6/2013
//
// Insert student's answer of a test question
function Auto_grade_answer($TestRecordID = null)
{
	if(isset($_POST['Func']))
	{
		$TestRecordID = $_POST['TestRecordID'];
		CallProcedure('auto_grade_answer', $TestRecordID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('auto_grade_answer', $TestRecordID);
}


// Created by: Dimas Widjaya
// Date: 3/6/2013
//
// Insert student's answer of a test question
function Grade_student_answer($QuestionID = null, $TestRecordID = null, $Points = null)
{
	if(isset($_POST['Func']))
	{
		$Points = $_POST['Points'];
		$QuestionID = $_POST['QuestionID'];
		$TestRecordID = $_POST['TestRecordID'];
		CallProcedure('grade_student_answer', $QuestionID, $TestRecordID, $Points);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('grade_student_answer', $QuestionID, $TestRecordID, $Points);
}

// Created by: Dimas Widjaya
// Date: 3/6/2013
//
// Assign total points of a particular test record and calculate the grade
function Grade_student_test($TestRecordID = null)
{
	if(isset($_POST['Func']))
	{

		$TestRecordID = $_POST['TestRecordID'];
		CallProcedure('grade_student_test', $TestRecordID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('grade_student_test', $TestRecordID);
}

// Created by: Dimas Widjaya
// Date: 3/3/2013
//
// Get the list of untaken test of a particular student
function Get_new_test($UserID = null)
{
	if(isset($_POST['Func']))
	{
		$UserID = $_POST['UserID'];
		CallProcedure('get_new_test', $UserID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('get_new_test', $UserID);
}

// Created by: Dimas Widjaya
// Date: 3/4/2013
//
// Get student's test answer 
function Get_student_test_answers($TestRecordID = null)
{
	$UserID = $_SESSION['userid'];
	if(isset($_POST['Func']))
	{
		$TestRecordID = $_POST['TestRecordID'];
		$UserID = $_POST['UserID'];
		CallProcedure('get_student_test_answers', $TestRecordID, $UserID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('get_student_test_answers', $TestRecordID, $UserID);
}

// Created by: Dimas Widjaya
// Date: 3/4/2013
//
// Get student's test answer 
function Get_test_record_info($TestRecordID = null)
{
	$UserID = $_SESSION['userid'];
	
	if(isset($_POST['Func']))
	{
		$TestRecordID = $_POST['TestRecordID'];
		$UserID = $_POST['UserID'];
		CallProcedure('get_test_record_info', $TestRecordID, $UserID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('get_test_record_info', $TestRecordID, $UserID);
}


// Created by: Dimas Widjaya
// Date: 3/9/2013
//
// Get the ungraded tests of a particular course 
function Get_ungraded_test($CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		CallProcedure('get_ungraded_test', $CourseID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('get_ungraded_test', $CourseID);
}

// Created by: Dimas Widjaya
// Date: 3/23/2013
//
// Get the ungraded tests of a particular course 
function Submit_test($TestRecordID = null,  $Pledge = null)
{
	if(isset($_POST['Func']))
	{
		$TestRecordID = $_POST['TestRecordID'];
		$Pledge = $_POST['Pledge'];
		CallProcedure('submit_test', $TestRecordID, $Pledge);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('submit_test', $TestRecordID, $Pledge);
}

// Created by: Dimas Widjaya
// Date: 3/23/2013
//
// Get the ungraded tests of a particular course 
function Make_grade_visible($TestRecordID = null)
{
	if(isset($_POST['Func']))
	{
		$TestRecordID = $_POST['TestRecordID'];
		CallProcedure('make_grade_visible', $TestRecordID);
		echo json_encode((object) null);
	}
	else 
		return CallProcedure('make_grade_visible', $TestRecordID);
}

// Created by: Dimas Widjaya
// Date: 3/27/2013
//
// Get the details of a students test record(for gradebook purpose)
function Get_student_test_details($TestRecordID = null)
{
	session_start();
	$UserID = $_SESSION['userid'];
	if(isset($_POST['Func']))
	{
		$TestRecordID = $_POST['TestRecordID'];
		echo json_encode(CallProcedure('get_student_test_details', $TestRecordID, $UserID));
	}
	else 
		return CallProcedure('get_student_test_details', $TestRecordID, $UserID);
}

// Created by: Dimas Widjaya
// Date: 3/28/2013
//
// Update test grade
function Update_test_point($TestRecordID = null, $NewPoints = null)
{
	session_start();
	$UserID = $_SESSION['userid'];
	if(isset($_POST['Func']))
	{
		$TestRecordID = $_POST['TestRecordID'];
		$NewPoints = $_POST['NewPoints'];
		echo json_encode(CallProcedure('update_test_point', $TestRecordID, $NewPoints, $UserID));
	}
	else 
		return CallProcedure('update_test_point', $TestRecordID, $NewPoints, $UserID);
}

// Created by: Dimas Widjaya
// Date: 3/29/2013
//
// Get all class average statistics for an instructor
function get_class_average()
{
	session_start();
	$UserID = $_SESSION['userid'];
	if(isset($_POST['Func']))
	{
		echo json_encode(CallProcedure('get_class_average', $UserID));
	}
	else 
		return CallProcedure('get_class_average', $UserID);
}




// Created by: Dominique Alons
// Date: 2/11/2013
//
// Get the courses that a particular teacher is teaching
function Get_teacher_courses($UserID = null)
{
	if(isset($_POST['Func']))
	{
		$UserID = $_POST['UserID'];
		echo json_encode(CallProcedure('teacher_courses', $UserID));
	}
	else 
		return CallProcedure('teacher_courses', $UserID);
		
}

// Created by: Scott Gildersleeve
// Date: 2/14/2013
//
// Get the information needed for class listings
function Get_class_info($UserID = null)
{
	if(isset($_POST['Func']))
	{
		$UserID = $_POST['UserID'];
		echo json_encode(CallProcedure('pop_class_arr', $UserID));
	}
	else 
		return CallProcedure('pop_class_arr', $UserID);
		
}

// Created by: Scott Gildersleeve
// Date: 2/14/2013
//
// Get the information needed for instructor gradebook
function Get_course_book($CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$CourseID = $_POST['CourseID'];
		echo json_encode(CallProcedure('get_course_book', $CourseID));
	}
	else 
		return CallProcedure('get_course_book', $CourseID);
		
}

// Created by: Scott Gildersleeve
// Date: 2/14/2013
//
// Get the information needed for the student grade book
function Get_student_book($UserID = null)
{
	if(isset($_POST['Func']))
	{
		session_start();
		$UserID = $_SESSION['userid'];
		echo json_encode(CallProcedure('get_student_gradebook', $UserID));
	}
	else 
		return CallProcedure('get_student_gradebook', $UserID);
		
}

// Created by: Scott Gildersleeve
// Date: 3/19/2013
//
// Update the test record information
function Update_test_rec($TestRecID = null, $TestGrade = null)
{
	if(isset($_POST['Func']))
	{
		$TestRecID = $_POST['TestRecID'];
		$TestGrade = $_POST['TestGrade'];
		echo json_encode(CallProcedure('test_rec_update', $TestRecID, $TestGrade));
	}
	else 
		return CallProcedure('test_rec_update', $TestRecID, $TestGrade);
	
}

// Created by: Scott Gildersleeve
// Date: 3/22/2013
//
// Get the information of the class
function Get_class_stats($CourseID = null)
{
	if(isset($_POST['Func']))
	{
		$TestRecID = $_POST['CourseID'];
		echo json_encode(CallProcedure('class_stats', $CourseID));
	}
	else 
		return CallProcedure('class_stats', $CourseID);
	
}

// Created by: Scott Gildersleeve
// Date: 3/28/2013
//
// Create a new test
function Create_test($CourseID = null, $TestName  = null, $Descript = null, $Vis       = null, $StartTime = null,
					 $EndTime  = null, $AutoGrade = null, $MaxPts   = null, $GivenTime = null, $ThePledge = null)
{
	
    
	
	
	if(isset($_POST['Func']))
	{
		session_start();
		$UserID = $_SESSION['userid'];
		echo json_encode(CallProcedure('add_test_proc', $CourseID, $TestName, $Descript, $Vis, $StartTime,
													    $EndTime, $AutoGrade, $MaxPts, $GivenTime, $ThePledge, $UserID));
	}
	else 
	{
		$UserID = $_SESSION['userid'];
		return CallProcedure('add_test_proc', $CourseID, $TestName, $Descript, $Vis, $StartTime,
											  $EndTime, $AutoGrade, $MaxPts, $GivenTime, $ThePledge, $UserID);
		
	}
	
}

// Created by: Scott Gildersleeve
// Date: 3/29/2013
//
// Add questions to a test being created
function Add_test_questions($QuestionType = null, $QuestionText = null, $PointsPoss = null)
{
	if(isset($_POST['Func']))
	{
		echo json_encode(CallProcedure('test_questions_proc', $QuestionType, $QuestionText, $PointsPoss));
	}
	else 
		return CallProcedure('test_questions_proc', $QuestionType, $QuestionText, $PointsPoss);
	
}

// Created by: Scott Gildersleeve
// Date: 3/29/2013
//
// Add a true and false type answer
function Add_true_false_question($AnsrText = null, $QuestionID = null)
{
	if(isset($_POST['Func']))
	{
		echo json_encode(CallProcedure('add_true_false', $AnsrText, $QuestionID));
	}
	else 
		return CallProcedure('add_true_false', $AnsrText, $QuestionID);
	
}

// Created by: Scott Gildersleeve
// Date: 3/29/2013
//
// Add a multiple choice or choose all or short answer answer
function Add_other_questions($QuestionID = null, $Correct = null, $AnsrText = null)
{
	if(isset($_POST['Func']))
	{
		echo json_encode(CallProcedure('add_questions', $QuestionID, $Correct, $AnsrText));
	}
	else 
		return CallProcedure('add_questions', $QuestionID, $Correct, $AnsrText);
	
}

// Created by: Scott Gildersleeve
// Date: 3/30/2013
//
// Add a multiple choice or choose all or short answer answer
function Add_test_records($UserID = null, $TestID = null)
{
	if(isset($_POST['Func']))
	{
		$UserID = $_POST['UserID'];
		echo json_encode(CallProcedure('distribute_test', $UserID, $TestID));
	}
	else 
		return CallProcedure('distribute_test', $UserID, $TestID);
	
}

if (!debug_backtrace())
{
	webservices_main();
}
?>